package controller.wechat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import entity.ModelData;
import entity.ModelResultEntity;
import entity.ModelVisitorEntity;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import service.Impl.WsUserImpl;
import util.api.ApIURL;
import util.httpclient.HttpService;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by thinkjoy on 2017/8/30.
 * 发送模板消息
 */
@Controller
@RequestMapping("/ws/msg/")
public class WeChatModelMsg {

    @Autowired
    private HttpService httpService;
    @Autowired
    private WsUserImpl wsUser;

    private Logger logger = Logger.getLogger(WeChatModelMsg.class);

    /**
     * 员工消息
     * @param modelResultEntity
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "sendmsg",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> SendMessage(ModelResultEntity modelResultEntity) throws ParseException {
        logger.info("接收结果:"+modelResultEntity.toString());
        Map<String, Object> res = new HashMap<>();
        try {
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String booktime=format.format(new Date());
            String openid = wsUser.findSingleUserInfo(modelResultEntity.getWorkerid());
            if (!StringUtils.isEmpty(openid)) {
                Map<String, Object> value = new HashMap<>();
                Map<String, Object> item = new HashMap<>();
                ModelData first = new ModelData("签到成功", "#173177");
                ModelData keynote1=new ModelData(modelResultEntity.getUsername(),"#173177");
                ModelData keynote2=new ModelData(booktime,"#173177");
                ModelData keynote3=new ModelData("正常","#173177");
                ModelData remark=new ModelData("可点击查看详情","#173177");
                value.put("touser", openid);
                value.put("template_id", ApIURL.MODEL_MSG_WORKER_SUCCESS);
                value.put("url","http://95128.zhits.com.cn:8888");
                item.put("first", first);
                item.put("keyword1",keynote1);
                item.put("keyword2",keynote2);
                item.put("keyword3",keynote3);
                item.put("remark",remark);
                value.put("data", item);
                String result = httpService.doPostAsJson(ApIURL.MODEL_MESSAGE_URL + ApIURL.ACCESS_TOKEN, JSON.toJSONString(value).toString());
                JSONObject object = JSON.parseObject(result);
                if (object.getInteger("errcode") == 0 && object.getString("errmsg").equals("ok")) {
                    res.put("status", "0");
                    res.put("msg", "消息推送成功");
                } else {
                    res.put("status", object.getInteger("errcode"));
                    res.put("msg", "失败！服务端请求微信出错");
                }
            } else {
                res.put("status", "1");
                res.put("msg", "该用户没有关注公众号，请尽快关注");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return res;
    }


    /**
     * 访客消息
     * @return
     */
    @RequestMapping(value = "sendvisitormsg",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> SendVisitorMsg(ModelVisitorEntity modelVisitorEntity){
        Map<String,Object> res=new HashMap<>();
        try {
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String booktime=format.format(new Date());
            String openid = wsUser.findSingleUserInfo(modelVisitorEntity.getWorkerid());
            if (!StringUtils.isEmpty(openid)) {
                Map<String, Object> value = new HashMap<>();
                Map<String, Object> item = new HashMap<>();
                ModelData first = new ModelData("您有一个访客到访，请接待", "#173177");
                ModelData keynote1=new ModelData(modelVisitorEntity.getVisitor(),"#173177");
                ModelData keynote2=new ModelData(modelVisitorEntity.getReason(),"#173177");
                ModelData keynote3=new ModelData(modelVisitorEntity.getPhone(),"#173177");
                ModelData keynote4=new ModelData(booktime,"#173177");
                ModelData remark=new ModelData("可点击查看详情","#173177");
                value.put("touser", openid);
                value.put("template_id", ApIURL.MODEL_MSG_VISITOR_SUCCESS);
                value.put("url","http://95128.zhits.com.cn:8888");
                item.put("first", first);
                item.put("keyword1",keynote1);
                item.put("keyword2",keynote2);
                item.put("keyword3",keynote3);
                item.put("keyword4",keynote4);
                item.put("remark",remark);
                value.put("data", item);
                String result = httpService.doPostAsJson(ApIURL.MODEL_MESSAGE_URL + ApIURL.ACCESS_TOKEN, JSON.toJSONString(value).toString());
                JSONObject object = JSON.parseObject(result);
                if (object.getInteger("errcode") == 0 && object.getString("errmsg").equals("ok")) {
                    res.put("status", "0");
                    res.put("msg", "消息推送成功");
                } else {
                    res.put("status", object.getInteger("errcode"));
                    res.put("msg", "失败！服务端请求微信出错");
                }
            } else {
                res.put("status", "1");
                res.put("msg", "接待者没有关注公众号，请尽快关注");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return res;
    }
}
